package com.website.main.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.website.common.vo.Tree;
import com.website.main.service.S_Power;
import com.website.main.util.Utils;
import com.website.mybatis.dao.PowerDao;
import com.website.mybatis.dao.RoleDao;
import com.website.mybatis.entity.SysPower;
import com.website.mybatis.entity.SysRole;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class S_PowerImpl implements S_Power {

	@Autowired
	PowerDao dao;

	@Autowired
	RoleDao roleDao;

	@Override
	public Map<String, Object> getAll(Boolean checked) {
		List<Tree> result = Utils.powerTOTreeMenu(dao.selectList(Wrappers.<SysPower>emptyWrapper()), checked);
		Map<String,Object> map = new HashMap<>();
		map.put("children", result);
		return map;
	}

	@Override
	public Map<String, Object> getByRole(Integer roleId) {
		Map<String,Object> map = new HashMap<>();
		SysRole s = roleDao.selectById(roleId);
		if(s == null){
			map.put("success", false);
			map.put("msg", "角色不存在");
			return map;
		}
		List<Integer> l = new ArrayList<>();
		for(SysPower p: s.getPowers()){
			l.add(p.getId());
		}
		map.put("data", l);
		map.put("success", true);
		return map;
	}

	@Override
	public Map<String, Object> update(SysPower p) {
		Map<String,Object> map = new HashMap<>();
		SysPower power = dao.selectById(p.getId());
		if(power == null){
			map.put("success", false);
			map.put("msg", "相应权限不存在");
			return map;
		}
		BeanUtils.copyProperties(p, power);
		if(dao.updateById(power) > 0){
			map.put("success", true);
			map.put("msg", "更新成功");
		}else{
			map.put("success", true);
			map.put("msg", "更新失败");
		}
		return map;
	}

	@Override
	public Map<String, Object> delete(Integer id) {
		Map<String,Object> map = new HashMap<>();
		SysPower p = dao.selectById(id);
		if(p == null){
			map.put("success", false);
			map.put("msg", "权限不存在");
			return map;
		}
		try {
			if(dao.deleteById(id) > 0){
				map.put("success", true);
				map.put("msg", "操作成功");
				return map;
			}
		} catch (DataIntegrityViolationException e) {
			map.put("success", false);
			map.put("msg", "删除失败，尚有角色关联此权限");
			return map;
		}
		map.put("success", false);
		map.put("msg", "操作失败");
		return map;
	}

	@Override
	public Map<String, Object> save(SysPower p) {
		Map<String,Object> map = new HashMap<>();
		if(dao.insert(p) > 0){
			map.put("success", true);
			map.put("id", p.getId());
			map.put("msg", "操作成功");
			return map;
		}
		map.put("success", false);
		map.put("msg", "操作失败");
		return map;
	}

	@Override
	public Map<String, Object> getById(Integer id) {
		Map<String,Object> map = new HashMap<>();
		SysPower p = dao.selectById(id);
		if(p != null){
			map.put("success", true);
			map.put("data", p);
			return map;
		}
		map.put("success", false);
		map.put("msg", "不存在");
		return map;
	}
}
